﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;
using System.Data;
using System.IO;
using System.Data.Common;

namespace blackhouse.WebNemo.Data.Stronger {
    public class SqlDataAccess : AdoDataAccess, ISupportSystemNames {

        #region Fields

        private MsSqlAdoDataHelper dataHelper;
        private static Dictionary<Type, DbType> predefinedTypes;

        #endregion

        #region Properties

        public SystemNames Names {
            get;
            set;
        }

        private static Dictionary<Type, DbType> PredefinedTypes {
            get {
                if (predefinedTypes == null)
                    predefinedTypes = GetPredefinedTypes();
                return predefinedTypes;
            }
        }

        #endregion

        #region Constructors

        public SqlDataAccess(string connectionString) {
            this.dataHelper = new MsSqlAdoDataHelper(connectionString);
            SqlVersioning versioning = new SqlVersioning(connectionString, typeof(SqlDataAccess).Assembly);
        }

        #endregion

        #region Methods

        private static Dictionary<Type, DbType> GetPredefinedTypes() {
            Dictionary<Type, DbType> types = new Dictionary<Type, DbType>();
            types.Add(typeof(Boolean), DbType.Byte);
            types.Add(typeof(DateTime), DbType.DateTime);
            types.Add(typeof(Decimal), DbType.Decimal);
            types.Add(typeof(Double), DbType.Double);
            types.Add(typeof(Guid), DbType.Guid);
            types.Add(typeof(Int16), DbType.Int16);
            types.Add(typeof(Int32), DbType.Int32);
            types.Add(typeof(Int64), DbType.Int64);
            types.Add(typeof(Single), DbType.Single);
            types.Add(typeof(String), DbType.String);
            types.Add(typeof(TimeSpan), DbType.Time);
            return types;
        }

        public override IDataReader GetReaderForGroupTabs(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupTabs";
            this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForColumnsSplit(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupColumnsSplit";
            this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override void SaveGroup(int id, string displayName, string identyficatorFormat, bool supportTransate, bool readOnly) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupSave";
            this.dataHelper.AddParameter("GroupId", id, DbType.Int32);
            this.dataHelper.AddParameter("DisplayName", displayName, DbType.String);
            this.dataHelper.AddParameter("Identyficator", String.IsNullOrEmpty(identyficatorFormat) ? DBNull.Value : ((object)identyficatorFormat), DbType.String);
            this.dataHelper.AddParameter("SupportTranslate", supportTransate, DbType.Boolean);
            this.dataHelper.AddParameter("ReadOnly", readOnly, DbType.Boolean);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void DeleteGroup(int id) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupDelete";
            this.dataHelper.AddParameter("GroupId", id, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetReaderForAllGroups() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupGetAll";
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForGroup(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupGet";
            this.dataHelper.AddParameter("Id", groupId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override bool CheckGroupName(string checkName) {
            return this.Names.GetGroupId(checkName) > 0;
        }

        public override DataTable GetGroupScheme(FromGroup[] groups) {
            RecordQuery q = new RecordQuery("pl-PL");
            q.SetTables(groups);
            this.dataHelper.Reset();
            this.dataHelper.Query = "SELECT TOP 1 * FROM " + q.GetFromQueryPart();
            return this.dataHelper.ExecuteScheme();
        }

        [Obsolete]
        public DataTable GetGroupScheme(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupSchemeGet";
            this.dataHelper.AddParameter("GroupId", groupId);
            return this.dataHelper.ExecuteScheme();
        }

        protected override IDataReader GetReaderForGroupColumns(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupColumnsGet";
            this.dataHelper.AddParameter("GroupId", groupId);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForGroupRecord(SelectColumn[] columns, int recordId, FromGroup[] groups, string language) {
            RecordQuery q = new RecordQuery(language);
            q.SetSelectColumns(columns);
            q.SetTables(groups);
            q.AddWhereClause(new WhereColumn(groups.First().TableName, "idx", recordId, typeof(Int32)));
            return q.Execute(this.dataHelper);
        }

        //protected override IDataReader GetReaderForGroupRecord(SelectColumn[] columns, int groupId, int recordId, string language, string groupType) {
        //    StringBuilder q = new StringBuilder("SELECT ");
        //    bool haveWhereClause = false;
        //    this.BuidQueryGroupRecord(q, columns, groupId, language, groupType, out haveWhereClause);
        //    if (haveWhereClause)
        //        q.Append(" AND");
        //    else
        //        q.Append(" WHERE");
        //    q.AppendFormat(" t.idx={0}", recordId);
        //    this.dataHelper.Reset();
        //    this.dataHelper.Query = q.ToString();
        //    return this.dataHelper.Execute();
        //}

        //protected IDataReader GetReaderForGroupRecords(SelectColumn[] columns, WhereColumn[] whereClauses, int groupId, string language, string groupType) {
        //    this.dataHelper.Reset();
        //    StringBuilder q = new StringBuilder("SELECT ");
        //    this.BuidQueryGroupRecord(q, columns, groupId, language, groupType);
        //    this.BuildWhereClauseGroupRecordAndAddParameters(whereClauses, q);
        //    this.dataHelper.Query = q.ToString();
        //    return this.dataHelper.Execute();
        //}

        protected override IDataReader GetReaderForGroupRecords(SelectColumn[] columns, WhereColumn[] whereClauses, FromGroup[] groups, string language) {
            RecordQuery rq = new RecordQuery(language);
            rq.SetSelectColumns(columns);
            rq.SetTables(groups);
            rq.SetWhereClauses(whereClauses);
            return rq.Execute(this.dataHelper);
        }

        protected override IDataReader GetReaderForGroupRecords(SelectColumn[] selectColumns, ProcedureQuery procedure) {
            procedure.AttachToDataHelper(this.dataHelper);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForRelatedGroupRecords(SelectColumn[] columns, FromGroup[] groups, WhereColumn[] whereClauses, string language) {
            RecordQuery rq = new RecordQuery(language);
            rq.SetSelectColumns(columns);
            rq.SetTables(groups);
            rq.SetWhereClauses(whereClauses);
            return rq.Execute(this.dataHelper);
        }

        //protected IDataReader GetReaderForRelatedGroupRecords(SelectColumn[] columns, int groupId, string columnName, int relGroupId, int relRecordId, string language, string groupType) {
        //    StringBuilder q = new StringBuilder("SELECT ");
        //    if (columns.Length > 0) {
        //        string[] columnsPrep = columns.Select(c => this.GetSelectColumn(c, language)).ToArray();
        //        q.Append(string.Join(", ", columnsPrep));
        //        q.Append(", ");
        //    }
        //    q.Append("t.idx");
        //    if (groupType != "ExtendedTable")
        //        q.Append(", t.identyfikator");
        //    if (groupType != "Table")
        //        q.Append(", t.ctime, t.mtime, t.cuser, t.muser");
        //    q.AppendFormat(" FROM {0} t INNER JOIN {1} r ON t.idx = r.rel", this.Names.GetSelectName(groupId), this.Names.GetSelectNameForRelation(groupId, columnName, relGroupId));
        //    q.AppendFormat(" WHERE r.idx={0}", relRecordId);
        //    if (groupType == "Table")
        //        q.Append(" AND t.del=0");
        //    if (groupType != "View")
        //        q.Append(" ORDER BY ctime");
        //    this.dataHelper.Reset();
        //    this.dataHelper.Query = q.ToString();
        //    return this.dataHelper.Execute();
        //}

        //private void BuildWhereClauseGroupRecordAndAddParameters(WhereColumn[] columns, StringBuilder sb) {
        //    foreach (WhereColumn wi in columns) {
        //        string pName = "P" + wi.ColumnName.ToUpper();
        //        sb.AppendFormat(" AND {0} = @{1}", wi.ColumnName, pName);
        //        this.dataHelper.AddParameter(pName, wi.Where, this.GetDbType(wi.Type));
        //    }
        //}

        //private DbType GetDbType(Type type) {
        //    if (PredefinedTypes.ContainsKey(type))
        //        return PredefinedTypes[type];
        //    else {
        //        object o = Activator.CreateInstance(type, new object[] { });
        //        System.Data.SqlClient.SqlParameter sp = new System.Data.SqlClient.SqlParameter("x", o);
        //        return sp.DbType;
        //    }
        //}

        //private void BuidQueryGroupRecord(StringBuilder q, SelectColumn[] columns, string groupTableName, string language, string groupType) {
        //    bool fakeParam;
        //    this.BuidQueryGroupRecord(q, columns, groupTableName, language, groupType, out fakeParam);
        //}

        //private void BuidQueryGroupRecord(StringBuilder q, SelectColumn[] columns, string groupTableName, string language, string groupType, out bool whereClause) {
        //    if (columns.Length > 0) {
        //        string[] columnsPrep = columns.Select(c => this.GetSelectColumn(c, language)).ToArray();
        //        q.Append(string.Join(", ", columnsPrep));
        //        q.Append(", ");
        //    }

        //    q.Append("idx");
        //    if (groupType != "ExtendedTable")
        //        q.Append(", identyfikator");
        //    if (groupType != "View")
        //        q.Append(", ctime, mtime, cuser, muser");
        //    q.AppendFormat(" FROM {0} t", groupTableName);
        //    if (groupType == "Table") {
        //        q.Append(" WHERE (t.del = 0 OR t.del IS NULL)");
        //        whereClause = true;
        //    }
        //    else
        //        whereClause = false;
        //}

        //private string GetSelectColumn(SelectColumn column, string language) {
        //    if (column.TranslateSupport)
        //        return string.Format("dbo.bhf_NemoTranslateGet({0}, t.idx, '{1}', t.{2}) as {2}", column.ColumnId, language, column.ColumnName);
        //    return column.ColumnName;
        //}

        public override void ChangeIdentyficator(int groupId, int recordId, string ident, int userId) {
            string tableName = this.Names.GetSelectName(groupId);
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoRecordIdentChange";
            this.dataHelper.AddParameter("Table", tableName);
            this.dataHelper.AddParameter("RecordId", recordId, DbType.Int32);
            this.dataHelper.AddParameter("Ident", ident);
            this.dataHelper.AddParameter("UserId", userId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void RemoveRecord(int groupId, int recordId, int userId) {
            string tableName = this.Names.GetSelectName(groupId);
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoRecordDelete";
            this.dataHelper.AddParameter("Table", tableName);
            this.dataHelper.AddParameter("RecordId", recordId, DbType.Int32);
            this.dataHelper.AddParameter("UserId", userId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void SaveGroupRecord(GroupRecordData data, int groupId, int recordId, int userId) {
            string groupTableName = this.Names.GetSelectName(groupId);
            this.dataHelper.Reset();

            DateTime time = DateTime.Now;
            StringBuilder buidQuery = new StringBuilder("UPDATE");
            buidQuery.AppendFormat(" {0} SET mtime=@mtime, muser={1}", groupTableName, userId);
            foreach (KeyValuePair<string, object> item in data) {
                buidQuery.AppendFormat(", {0}=@{0}", item.Key);
                GroupRecordDataValue value = item.Value as GroupRecordDataValue;
                this.dataHelper.AddParameter(item.Key, value.Value, value.DataType);
            }
            buidQuery.AppendFormat(" WHERE idx={0}", recordId);
            this.dataHelper.Query = buidQuery.ToString();
            this.dataHelper.AddParameter("mtime", time, DbType.DateTime);
            this.dataHelper.ExecuteWithoutResult();

            this.dataHelper.Reset();

        }

        public override int AddGroupRecord(GroupRecordData data, int groupId, int userId) {
            string selectName = this.Names.GetSelectName(groupId);
            DateTime time = DateTime.Now;
            this.dataHelper.Reset();
            this.dataHelper.AddParameter("ctime", time, DbType.DateTime);
            this.dataHelper.AddParameter("mtime", time, DbType.DateTime);
            StringBuilder buidQuery = new StringBuilder();
            StringBuilder buidQueryColumns = new StringBuilder();
            StringBuilder buidQueryColumnValues = new StringBuilder();
            buidQueryColumns.Append("ctime, mtime, cuser, muser");
            buidQueryColumnValues.AppendFormat("@ctime, @mtime, {0}, {0}", userId);
            foreach (KeyValuePair<string, object> item in data) {
                buidQueryColumns.Append(", " + item.Key);
                buidQueryColumnValues.Append(", @" + item.Key);
                GroupRecordDataValue value = item.Value as GroupRecordDataValue;
                this.dataHelper.AddParameter(item.Key, value.Value, value.DataType);
            }
            buidQuery.AppendFormat("INSERT INTO {0}({1}) VALUES ({2}) SELECT @@IDENTITY", selectName, buidQueryColumns, buidQueryColumnValues);
            this.dataHelper.Query = buidQuery.ToString();
            object oRecId = this.dataHelper.ExecuteScalar();
            if (!(oRecId is DBNull))
                return Convert.ToInt32(oRecId);
            else {
                if (data.ContainsKey("idx"))
                    return (int)((GroupRecordDataValue)data["idx"]).Value;
                else
                    return 0;
            }
        }

        public override void AddRelationToGroupRecord(int relGroupId, int relRecordId, int parentGroupId, string parentColumnName, int parentRecordId, int userId) {
            StringBuilder buildQuery = new StringBuilder();
            buildQuery.AppendFormat(@"INSERT INTO {0}(idx, rel, ctime, cuser) 
SELECT {2}, {1}, GETDATE(), {3}  
	WHERE NOT EXISTS( 
		SELECT 
			1 
		FROM
			{0} 
		WHERE
			rel = {1} 
			AND idx = {2})", this.Names.GetSelectNameForRelation(relGroupId, parentColumnName, parentGroupId), relRecordId, parentRecordId, userId);
            this.dataHelper.Reset();
            this.dataHelper.Query = buildQuery.ToString();
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void RemoveRelationToGroupRecord(int relGroupId, int relRecordId, int parentGroupId, string parentColumnName, int parentRecordId) {
            StringBuilder buildQuery = new StringBuilder();
            buildQuery.AppendFormat("DELETE FROM {0} WHERE rel={1} AND idx={2}", this.Names.GetSelectNameForRelation(relGroupId, parentColumnName, parentGroupId), relRecordId, parentRecordId);
            this.dataHelper.Reset();
            this.dataHelper.Query = buildQuery.ToString();
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void SaveGroupRecordFile(int groupId, int recordId, string column, string fileName, System.IO.Stream stream, long streamSize, int userId) {
            if (stream.CanSeek)
                stream.Seek(0, System.IO.SeekOrigin.Begin);

            StringBuilder buildQuery = new StringBuilder();
            buildQuery.AppendFormat("UPDATE {0} SET {1}_data = @Data, {1}_hash = NEWID(), {1}_name = @Name,  {1}_size = {2}, mtime = GETDATE(), muser={4} WHERE idx = {3}", this.Names.GetSelectName(groupId), column, streamSize, recordId, userId);

            this.dataHelper.Reset();
            this.dataHelper.Query = buildQuery.ToString();

            int bufferLen = 8040;
            using (BinaryReader br = new BinaryReader(stream)) {
                byte[] chunk = br.ReadBytes(bufferLen);


                this.dataHelper.AddParameter("Data", chunk, DbType.Binary);
                this.dataHelper.AddParameter("Name", fileName);
                this.dataHelper.ExecuteWithoutResult();

                buildQuery = new StringBuilder();
                buildQuery.AppendFormat("UPDATE {0} SET {1}_data.WRITE(@Data, NULL, 0) WHERE idx = {3}", this.Names.GetSelectName(groupId), column, streamSize, recordId);

                chunk = br.ReadBytes(bufferLen);

                while (chunk.Length > 0) {
                    this.dataHelper.Reset();
                    this.dataHelper.Query = buildQuery.ToString();
                    DbParameter paramData = this.dataHelper.AddParameter("Data", chunk, DbType.Binary);
                    this.dataHelper.ExecuteWithoutResult();
                    chunk = br.ReadBytes(bufferLen);
                }
                br.Close();
            }
        }

        public override IDataReader GetReaderForNemoUsers() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoUsers";
            return this.dataHelper.Execute();
        }

        public override IDataReader GetReaderForGroupSystemNames() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupSystemNamesGet";
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetReaderForAvalibleLanguages() {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoTranslateGetLanguages";
            return this.dataHelper.Execute();
        }

        public override string GetOryginalTranslate(int groupId, int fieldId, int recordId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoTranslateGetOryginal";
            this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
            this.dataHelper.AddParameter("FieldId", fieldId, DbType.Int32);
            this.dataHelper.AddParameter("RecordId", recordId, DbType.Int32);
            return Convert.ToString(this.dataHelper.ExecuteScalar());
        }

        public override string GetTranslate(int groupId, int fieldId, int recordId, string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoTranslateGet";
            this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
            this.dataHelper.AddParameter("FieldId", fieldId, DbType.Int32);
            this.dataHelper.AddParameter("RecordId", recordId, DbType.Int32);
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength);
            return Convert.ToString(this.dataHelper.ExecuteScalar());
        }

        public override void SetTranslate(int groupId, int fieldId, int recordId, string language, string value, int userId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoTranslateSet";
            this.dataHelper.AddParameter("FieldId", fieldId, DbType.Int32);
            this.dataHelper.AddParameter("Record", recordId, DbType.Int32);
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength);
            this.dataHelper.AddParameter("Translate", value, DbType.String);
            this.dataHelper.AddParameter("User", userId, DbType.Int32);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override void RemoveTranslate(int groupId, int fieldId, int recordId, string language) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoTranslateRemove";
            this.dataHelper.AddParameter("FieldId", fieldId, DbType.Int32);
            this.dataHelper.AddParameter("Record", recordId, DbType.Int32);
            this.dataHelper.AddParameter("Language", language, DbType.StringFixedLength);
            this.dataHelper.ExecuteWithoutResult();
        }

        protected override IDataReader GetReaderForColumnValueValidators(int columnId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoColumnValidators";
            this.dataHelper.AddParameter("ColumnId", columnId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override string GetCustomEditControlConfiguration(int columnId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoColumnsEditConfiguration";
            this.dataHelper.AddParameter("ColumnId", columnId, DbType.Int32);
            object conf = this.dataHelper.ExecuteScalar();
            if (conf == null)
                return string.Empty;
            else
                return Convert.ToString(conf);
        }

        protected override IDataReader GetReaderFileGuids(string tableName, string columnName) {
            this.dataHelper.Reset();
            this.dataHelper.Query = string.Format("SELECT {0}_hash as [guid], idx as [id] FROM {1} WHERE {0}_data IS NOT NULL", columnName, tableName);
            return this.dataHelper.Execute();
        }

        public override byte[] GetFileStream(string columnName, int tableId, int recordId) {
            return this.GetFileStream(columnName, this.Names.GetSelectName(tableId), recordId);
        }

        public override byte[] GetFileStream(string columnName, string tableName, int recordId) {
            this.dataHelper.Reset();
            this.dataHelper.Query = string.Format("SELECT {0}_data FROM {1} WHERE idx = {2} AND {0}_data IS NOT NULL", columnName, tableName, recordId);
            return this.dataHelper.ExecuteScalar<byte[]>();
        }

        protected override IDataReader GetFileInfoReader(string columnName, int tableId, int recordId) {
            return this.GetFileInfoReader(columnName, this.Names.GetSelectName(tableId), recordId);
        }

        protected override IDataReader GetFileInfoReader(string columnName, string tableName, int recordId) {
            this.dataHelper.Reset();
            this.dataHelper.Query = string.Format("SELECT {0}_name as [name], {0}_size as [size], {0}_hash as [hash] FROM {1} WHERE idx = {2} AND {0}_data IS NOT NULL", columnName, tableName, recordId);
            return this.dataHelper.Execute();
        }

        protected override IDataReader GetExtensionsReader(int groupId) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupExtensions";
            this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
            return this.dataHelper.Execute();
        }

        public override void SaveExtensions(int groupId, string groupExtension, string groupRecordExtension) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupExtensionsSave";
            this.dataHelper.AddParameter("GroupId", groupId, DbType.Int32);
            this.dataHelper.AddParameter("GroupExtension", groupExtension, DbType.String);
            this.dataHelper.AddParameter("RecordExtension", groupRecordExtension, DbType.String);
            this.dataHelper.ExecuteWithoutResult();
        }

        public override int CreateGroup(string systemName) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupCreate";
            this.dataHelper.AddParameter("SystemName", systemName, DbType.String);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        public override bool CheckViewDeclaration(string selectDeclaration) {
            this.dataHelper.Reset();
            this.dataHelper.Query = selectDeclaration;
            try {
                DataTable dt = this.dataHelper.ExecuteScheme();
                DataColumn dc_idx = dt.Columns["Idx"];
                DataColumn dc_ident = dt.Columns["Identyfikator"];
                return dc_idx != null && dc_ident != null && dc_idx.DataType.Equals(typeof(Int32)) && dc_ident.DataType.Equals(typeof(String));
            }
            catch (Exception) {
                return false;
            }
        }

        public override int CreateView(string systemName, string selectDeclaration) {
            this.dataHelper.Reset();
            this.dataHelper.ProcedureName = "bhp_NemoGroupViewCreate";
            this.dataHelper.AddParameter("SystemName", systemName);
            this.dataHelper.AddParameter("SelectDeclaration", selectDeclaration);
            return Convert.ToInt32(this.dataHelper.ExecuteScalar());
        }

        #endregion

    }
}
